package com.example.administrator.jingpingou.fragment;

import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.adapter.TeJiaAdapterOne;
import com.example.administrator.jingpingou.adapter.TeJiaAdapterTwo;
import com.example.administrator.jingpingou.bean.TeJiaBean;
import com.example.administrator.jingpingou.config.NetConfig;
import com.example.administrator.jingpingou.netinteface.NetInterface;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/8/28 0028.
 */
public class TeJiaFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ImageView mImageViewZhuan,mImageViewFen,mImageViewPig;
    private List<TeJiaBean.ListEntity> mData;
    private Retrofit mRetrofit;
    private NetInterface mNetInterface;
    private TeJiaAdapterOne mAdapter;
    private TeJiaAdapterTwo mAdapterTwo;
    private int mCurrentPage=1;
    private GridLayoutManager mManager;
    private boolean flag=true;
    private LinearLayoutManager mLinearManager;
    private int mCurrentPageTwo=1;
    private View mViewListView;
    private ListView mListView;
    private ArrayAdapter mListAdapter;
    private int mCid;
    private String[] ListData={ "时尚女装",
            "品质男装"
            ,"男鞋女鞋"
            ,"包包配饰"
            ,"美容护肤"
            ,"数码家电"
           , "日用百货"
           , "美食特产"
           , "母婴儿童"
            ,"车品户外"
           , "舒适内衣"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tejia,null);
        iniView(view);
        iniData();
        setData();
        setListener();
        return view;
    }


    private void setListener() {
        mImageViewFen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListView.getVisibility()==View.INVISIBLE) {
                    mListView.setVisibility(View.VISIBLE);
                    setAnimShow();

                }else {
                    setAnimDismiss();
                    mListView.setVisibility(View.INVISIBLE);
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mData.clear();
                if(flag) {
                    mAdapter=new TeJiaAdapterOne(mData,getActivity());
                }else{
                    mAdapterTwo=new TeJiaAdapterTwo(mData,getActivity());
                }
                loadResult(1,position);
                mCid=position;
                setData();
            }
        });
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadResult(1,mCid);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                    if(newState==RecyclerView.SCROLL_STATE_IDLE) {
                        if(flag) {
                            int lastVisibleItemPosition = mManager.findLastVisibleItemPosition();
                            if(lastVisibleItemPosition==mData.size()-1) {
                                mCurrentPage++;
                                loadResult(mCurrentPage,0);
                            }

                        }else {
                            int lastVisibleItemPosition1 = mLinearManager.findLastVisibleItemPosition();
                            if(lastVisibleItemPosition1==mData.size()-1) {
                                mCurrentPageTwo++;
                                Log.i("TAG", "====mcurrent"+mCurrentPageTwo);
                                loadResult(mCurrentPageTwo,0);
                            }

                        }

                    }


            }


            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

      setZhuanHuan();

    }

    private void setAnimDismiss() {
        TranslateAnimation anim=new TranslateAnimation(Animation.RELATIVE_TO_SELF
                ,0,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,
                0);
        anim.setDuration(1000);
        anim.setRepeatMode(Animation.REVERSE);
        mListView.setAnimation(anim);
        anim.start();
        anim.setFillAfter(true);
    }

    private void setAnimShow() {
        TranslateAnimation anim=new TranslateAnimation(Animation.RELATIVE_TO_SELF
                ,1,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,
                0);
        anim.setDuration(1000);
        anim.setRepeatMode(Animation.REVERSE);
        mListView.setAnimation(anim);
        anim.start();
        anim.setFillAfter(true);
    }

    private void setZhuanHuan() {
        mImageViewZhuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mCurrentPage=1;
                if(flag) {
                    mData.clear();
                    flag=false;
                    mImageViewZhuan.setImageResource(R.mipmap.change2);
                    mAdapterTwo=new TeJiaAdapterTwo(mData,getActivity());
                    loadResult(mCurrentPageTwo,0);

                }else{
                    mData.clear();
                    mImageViewZhuan.setImageResource(R.mipmap.change1);
                    mAdapter=new TeJiaAdapterOne(mData,getActivity());
                    flag=true;
                    loadResult(mCurrentPage,0);
                }

                setData();
            }
        });
    }

    private void setData() {
        if(flag) {
            mManager= new GridLayoutManager(getActivity(),2,GridLayoutManager.VERTICAL,false);
            mRecyclerView.setLayoutManager(mManager);
            mRecyclerView.setAdapter(mAdapter);

        }else{

           mLinearManager =new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
            mRecyclerView.setLayoutManager(mLinearManager);
            mRecyclerView.setAdapter(mAdapterTwo);
        }
        mSwipeRefreshLayout.setColorSchemeColors(Color.GRAY,Color.GREEN,Color.BLACK);

    }

    private void iniData() {
//       mViewListView =  LayoutInflater.from(getActivity()).inflate(R.layout.fenlei_tejia_show, null);
//        mListView= (ListView) mViewListView.findViewById(R.id.lv_fenlei_show);
        mImageViewPig.setBackgroundResource(R.drawable.pig_fly_wait);
        AnimationDrawable md= (AnimationDrawable) mImageViewPig.getDrawable();
        md.start();
        mListAdapter =new ArrayAdapter(getActivity(),R.layout.item_fenlei_tejia,ListData);
        mListView.setAdapter(mListAdapter);
        mRetrofit=new Retrofit.Builder().baseUrl(NetConfig.TEJIA_FRAGMENT_PATH)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mNetInterface=mRetrofit.create(NetInterface.class);
        mData=new ArrayList<>();
        mAdapter=new TeJiaAdapterOne(mData,getActivity());
        loadResult(mCurrentPage,0);

    }

    private void loadResult(final int position,int cid) {
        Call<TeJiaBean> bodyCall=null;
        if(cid==0) {
           bodyCall=mNetInterface.getNetTeJiaWithBean(""+position);

        }else{
//            Map  map=new HashMap();
////              map.put("page",position);
//             map.put("cid",cid);
            bodyCall=mNetInterface.getNetTeJiaFenLeiWithBean(cid+1);
            Log.i("TAG", "====bodyCall"+bodyCall);
        }
        bodyCall.enqueue(new Callback<TeJiaBean>() {
            @Override
            public void onResponse(Call<TeJiaBean> call, Response<TeJiaBean> response) {
                TeJiaBean teJiaBean = response.body();
                List<TeJiaBean.ListEntity> list = teJiaBean.getList();
                if(position==1) {
                    mData.clear();
                }
                if(mAdapter==null) {

                    mAdapterTwo.notifyDataSetChanged();
                }else{
                    mAdapter.notifyDataSetChanged();
                }
                mData.addAll(list);

                mImageViewPig.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<TeJiaBean> call, Throwable t) {

            }
        });
    }

    private void iniView(View view) {
        mRecyclerView= (RecyclerView) view.findViewById(R.id.rv_tejia_show);
        mSwipeRefreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.sl_tejia_fresh);
        mImageViewZhuan= (ImageView) view.findViewById(R.id.iv_tejia_zhuanhuan);
        mImageViewFen= (ImageView) view.findViewById(R.id.iv_tejia_fenlei);
        mListView= (ListView) view.findViewById(R.id.lv_tejia_show);
        mImageViewPig= (ImageView) view.findViewById(R.id.iv_tejia_anim);
    }
}
